const fs = require('fs');
const { getThemeAll, getThemeSections, getBlockSchema } = require('../service/section');
const { writeFile, readFile } = require('../util/index');
const defaultGlobal = require('../../config/default/global');
const { getGlobal } = require('../../config/src/setting/global');

function getBlocks(data) {
  const config = JSON.parse(fs.readFileSync(`./app/theme/${data.themeName}/config.json`, 'utf-8') || '[]');
  return config;
}

exports.addBlock = function addBlock(data) {
  const content = getBlocks({ themeName: 'temp1' });
  fs.writeFileSync('./app/theme/temp1/config.json', JSON.stringify(content.concat(data)));
  return true;
};

exports.delBlock = function delBlock(data) {
  const content = getBlocks({ themeName: 'temp1' });
  fs.writeFileSync('./app/theme/temp1/config.json', JSON.stringify(content.filter((item) => item.id !== data.id)));

  return true;
};

exports.updateBlock = function updateBlock(data) {
  const content = getBlocks({ themeName: 'temp1' });
  fs.writeFileSync(
    './app/theme/temp1/config.json',
    JSON.stringify(
      content.map((item) => {
        if (item.id === data.id) {
          return data;
        }
        return item;
      })
    )
  );
  return true;
};

exports.updateBlockAll = function updateBlockAll({ blocks }) {
  fs.writeFileSync('./app/theme/temp1/config.json', JSON.stringify(blocks));
  return true;
};

exports.getThemeAllAndSections = function(data) {
  const themes = getThemeAll();
  const allThemeSection = {};
  themes.forEach((theme) => {
    allThemeSection[theme] = getThemeSections(theme);
  });

  return allThemeSection;
};

// 获取所有的积木块
function getCardAll(data) {
  const dir = './src/theme/component/sections';

  const sections = fs.readdirSync(dir);
  const sectionsAll = {};
  sections.forEach((file) => {
    if (file.indexOf('.liquid') == -1) {
      const cards = fs.readdirSync(dir + '/' + file);
      sectionsAll[file] = {};
      cards.forEach((cardFile) => {
        const schema = getBlockSchema(dir + '/' + file + '/' + cardFile);
        const cardFileName = cardFile.replace('.liquid', '');
        schema['router'] = `${file}:${cardFileName}`;
        sectionsAll[file][cardFileName] = schema;
      });
    }
  });

  return sectionsAll;
}
exports.getCardAll = getCardAll;
exports.getBlocks = getBlocks;

exports.paymentform = function () {
  return readFile('./php_pay/payoneer.html')
    .replace(
      '{$this->payment->interface_type_param->key}',
      'pk_test_51MM13qIuSh5sFyYa8N2bdstTiCHqX0g9eX7XubgJmOgXhejBha1h05zmqkD8cDhgHf5k7lQmGHBM3lxwD8pPDTtS00qrJo0GvV'
    )
    .replace('{$currency}', 'gbp')
    .replace('$payamount', 4127);
};


exports.getGlobalConfig = function(req, res, next) {
  if (req.params.id === 'basic') {
    res.json({ data: defaultGlobal }).end();
  }
  res.json({ data: getGlobal(req.params.id) }).end();
};


exports.getSectionsConfig = function(req, res, next) {
  const query = req.query;
  if (!query.sectionName || !query.template) {
    res.json({ data: '' }).end();
    return;
  }

  const schema = getBlockSchema(`./src/theme/${query.template}/sections/${query.sectionName}.liquid`);
  const global = getGlobal(query.template);
  res.json({ data: { schema,global } }).end();
};


